package com.whales.shared.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.whales.shared.model.LoginVO;
import com.whales.shared.service.CommonService;
import com.whales.shared.service.LoginService;
import com.whales.shared.util.SessionUtil;

@Controller
public class LoginController {
    @Inject
    private LoginService loginService;
    @Inject
    private CommonService commonService;

    /**
     * Login
     * @메소드명 : checkLogin   					   
     * @작성자 : sw775.park   
     * @작성일 : 2012. 3. 2.
     * @param request
     * @param model
     * @param login
     * @return
     * @throws Exception	
     * @설명 :
     * @수정이력 - 수정일,수정자,수정내용					   
     *										   
     */
    @RequestMapping("checkLogin.do")
    public String checkLogin(HttpServletRequest request, LoginVO login, Model model) throws Exception {
    	LoginVO checkedLoginVO = loginService.findInfo(login);
    	if(checkedLoginVO == null){
    		model.addAttribute("loginId", login.getId());
    		model.addAttribute("loginStatus", "fail");
    		return "login";
    	}
    	checkedLoginVO.setLoginIp(request.getRemoteAddr());// 최근 로그인 정보
        SessionUtil.setAttribute("login", checkedLoginVO); // 로그인 정보를 Sesstion에 저장한다.
        SessionUtil.setAttribute("contextPath", request.getContextPath());// ContextPath
        SessionUtil.setAttribute("property", commonService.getProperties());
        SessionUtil.setAttribute("loginKey", loginService.createLogin(checkedLoginVO));
        return "index";
    }
    
    /**
     * Logout
     * @메소드명 : logout   					   
     * @작성자 : sw775.park   
     * @작성일 : Nov 6, 2009
     * @param request
     * @throws Exception	
     * @설명 :
     * @수정이력 - 수정일,수정자,수정내용					   
     *										   
     */
    @RequestMapping("logout.do")
    public String logout(HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:/login.do";
    }
}
